package com.tanhua.app.service;

import com.alibaba.fastjson.JSONObject;
import com.google.j2objc.annotations.ObjectiveCName;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.SoulApi;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.BaoGao;
import com.tanhua.domain.mongo.Dimension;
import com.tanhua.domain.mongo.SimilarYou;
import com.tanhua.domain.mongo.Soul;
import com.tanhua.domain.vo.AnswersVo;
import com.tanhua.domain.vo.BaoGaoVo;
import com.tanhua.domain.vo.QuestionsVo;
import com.tanhua.domain.vo.SoulVo;
import org.apache.commons.lang3.RandomUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SoulService {
    @Reference
    private SoulApi soulApi;
    @Reference
    private UserInfoApi userInfoApi;

    public ResponseEntity testSoul() {
        List<SoulVo> soulVoList = new ArrayList<>();
        List<Soul> souls = soulApi.findSoul();
        for (Soul soul : souls) {
            SoulVo soulVo = new SoulVo();

            BeanUtils.copyProperties(soul,soulVo);
            soulVo.setId(soul.getId().toHexString());
            List<QuestionsVo> voList = soulVo.addQuestions(soul.getQuestions());
            soulVo.setQuestions(voList);
            if (soul.getReportId()!=null){
                soulVo.setReportId(soul.getReportId().toHexString());
            }
            soulVoList.add(soulVo);
        }
        for (SoulVo soulVo : soulVoList) {
            System.out.println(soulVo);
        }
        return ResponseEntity.ok(soulVoList);
    }

    public ResponseEntity commitSoulTest(List<Map> answers) {
        int core= 0;
        List<AnswersVo> answersVoList = new ArrayList<>();
        for (Map answer : answers) {
            AnswersVo answersVo = new AnswersVo();
            String questionId = (String) answer.get("questionId");
            String optionId = (String) answer.get("optionId");
            answersVo.setQuestionId(questionId);
            answersVo.setOptionId(optionId);
            answersVoList.add(answersVo);
        }
        for (AnswersVo answer : answersVoList) {
            String optionId = answer.getOptionId();
            String questionId = answer.getQuestionId();

            core += soulApi.findSoulQuestionCore(questionId,optionId);
        }

        BaoGao baoGao = getBaoGao(core);

        soulApi.setReportId(answersVoList.get(0).getQuestionId(),baoGao);
        soulApi.addBaoGao(baoGao);
        BaoGaoVo baoGaoVo = new BaoGaoVo();
        BeanUtils.copyProperties(baoGao,baoGaoVo);
        baoGaoVo.setId(baoGao.getId().toHexString());
        return ResponseEntity.ok(baoGaoVo.getId());
    }
    public BaoGao getBaoGao(int core){
        BaoGao baoGao = new BaoGao();
        baoGao.setId(new ObjectId());
        baoGao.setUserId(UserHolder.getUser().getId());
        if (core<=21&&core>0){
            baoGao.setConclusion("猫头鹰：他们的共同特质为重计划、条理、细节精准。在行为上，表现出喜欢理性思考与分析、较重视制度、结构、规范。他们注重执行游戏规则、循规蹈矩、巨细靡遗、重视品质、敬业负责。");
            baoGao.setCover("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/test_soul/owl.png");
            baoGao.setDimensions(getDimensions());
            baoGao.setSimilarYou(getSimilarYou());
        }else if (core>21&&core<=41){
            baoGao.setConclusion("白兔型：平易近人、敦厚可靠、避免冲突与不具批判性。在行为上，表现出不慌不忙、冷静自持的态度。他们注重稳定与中长程规划，现实生活中，常会反思自省并以和谐为中心，即使面对困境，亦能泰然自若，从容应付。");
            baoGao.setCover("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/test_soul/rabbit.png");
            baoGao.setDimensions(getDimensions());
            baoGao.setSimilarYou(getSimilarYou());
        }else if (core>41&&core<=55){
            baoGao.setConclusion("狐狸型 ：人际关系能力极强，擅长以口语表达感受而引起共鸣，很会激励并带动气氛。他们喜欢跟别人互动，重视群体的归属感，基本上是比较「人际导向」。由于他们富同理心并乐于分享，具有很好的亲和力，在服务业、销售业、传播业及公共关系等领域中，狐狸型的领导者都有很杰出的表现。");
            baoGao.setCover("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/test_soul/fox.png");
            baoGao.setDimensions(getDimensions());
            baoGao.setSimilarYou(getSimilarYou());
        }else {
            baoGao.setConclusion("狮子型：性格为充满自信、竞争心强、主动且企图心强烈，是个有决断力的领导者。一般而言，狮子型的人胸怀大志，勇于冒险，看问题能够直指核心，并对目标全力以赴。他们在领导风格及决策上，强调权威与果断，擅长危机处理，此种性格最适合开创性与改革性的工作。");
            baoGao.setCover("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/test_soul/lion.png");
            baoGao.setDimensions(getDimensions());
            baoGao.setSimilarYou(getSimilarYou());
        }
        return baoGao;
    }

    public ResponseEntity report(String id) {
        BaoGao baoGao = soulApi.report(new ObjectId(id));
        List<SimilarYou> similarYou = baoGao.getSimilarYou();
        for (SimilarYou you : similarYou) {
            System.out.println(you);
        }
        return ResponseEntity.ok(baoGao);
    }
    public List<SimilarYou> getSimilarYou(){
        List<SimilarYou> similarYouList = new ArrayList<>();
        for (Long i = 1L; i <= 10; i++) {
            SimilarYou similarYou = new SimilarYou();
            UserInfo userInfo = userInfoApi.findUserInfoVo(i);
            BeanUtils.copyProperties(userInfo,similarYou);
            similarYou.setId(userInfo.getId().intValue());
            similarYou.setAvatar(userInfo.getAvatar());
            similarYouList.add(similarYou);
        }
        return similarYouList;
    }
    public List<Dimension> getDimensions(){
        List<Dimension> DimensionList = new ArrayList<>();
        String[] keys={"外向","判断","抽象","理性"};
        for (String key : keys) {
            Dimension dimension = new Dimension();
            dimension.setKey(key);
            dimension.setValue(RandomUtils.nextInt(0,99)+"%");
            DimensionList.add(dimension);
        }
        return DimensionList;
    }

}
